package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.ui.activity.InvitationLetterActivity;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

/**
 * Created by apple on 2017/5/4.
 */

public class ActivityPicPagerAdapter extends PagerAdapter {
    private final String tag;
    private int type;
    private List<String> picList;
    private Context context;

    public ActivityPicPagerAdapter(Context context, List<String> picList, int type, String tag) {
        this.context = context;
        this.picList = picList;
        this.type = type;
        this.tag = tag;
    }

    @Override
    public int getCount() {
        return picList == null ? 0 : picList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);

    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final ImageView imageView = new ImageView(context);
        if (InvitationLetterActivity.TAG.equals(tag)) {
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }else {
//            imageView.setScaleType(ImageView.ScaleType.MATRIX);
        }
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        int screenWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽（像素，如：480px）
//        L.e("image:" + screenWidth);
        if (type == InvitationLetterActivity.EDITOR) {
            Picasso.with(App.getInstance())
                    .load(new File(picList.get(position)))
//                    .resize(screenWidth, screenWidth / 5 * 3)
//                    .centerCrop()
                    .error(R.mipmap.activity_loading)
                    .placeholder(R.mipmap.activity_loading)
                    .into(imageView);
        } else if (type == InvitationLetterActivity.ACTIVITY) {
            Picasso.with(App.getInstance())
                    .load(picList.get(position))
//                    .resize(screenWidth, screenWidth / 5 * 3)
//                    .centerCrop()
                    .error(R.mipmap.activity_loading)
                    .placeholder(R.mipmap.activity_loading)
                    .into(imageView);
        }
        container.addView(imageView, 0);
        if (onItemClickListener != null) {
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(position);
                }
            });
            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickListener.onItemLongClick(position);
                    return false;
                }
            });

        }
        return imageView;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onItemLongClick(int position);
    }
}
